package org.nlp2rdf.navigator.benchmark;

import org.aksw.commons.util.strings.BifContains;
import org.mindswap.pellet.utils.PermutationGenerator;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Sebastian Hellmann <hellmann@informatik.uni-leipzig.de>
 */
public class NavigationExampleFinder {

    public static void main(String[] args) {
        String s = "";
        s = "http://dbpedia.org/resource/Category:LGBT_television_personalities";
        s = "http://dbpedia.org/resource/Category:Alumni_of_Magdalen_College%2C_Oxford";
        System.out.println(getSearchQueries(s));
        System.out.println(getNavigationQueries("http://dbpedia.org/resource/Alfred_Jules_Ayer",s));
    }


    // public static void

    /**
     * gets the words from the category URIs and makes queries searching for instances,
     * which have the words in their rdf:comment
     * It is highly likely that something will be found with the last query:
     * <p/>
     * http://dbpedia.org/resource/Category:Alumni_of_Magdalen_College%2C_Oxford ->
     * <p/>
     * [Select ?s { ?s rdfs:comment ?comment . FILTER (bif:contains (?comment, '"Alumni" and "of" and "Magdalen" and "College" and "Oxford"' )) } LIMIT 100,
     * Select ?s { ?s rdfs:comment ?comment . FILTER (bif:contains (?comment, '"of" and "Magdalen" and "College" and "Oxford"' )) } LIMIT 100,
     * Select ?s { ?s rdfs:comment ?comment . FILTER (bif:contains (?comment, '"Magdalen" and "College" and "Oxford"' )) } LIMIT 100,
     * Select ?s { ?s rdfs:comment ?comment . FILTER (bif:contains (?comment, '"College" and "Oxford"' )) } LIMIT 100,
     * Select ?s { ?s rdfs:comment ?comment . FILTER (bif:contains (?comment, '"Oxford"' )) } LIMIT 100]
     *
     * @param categoryURI
     * @return a list of sparql queries
     */
    public static List<String> getSearchQueries(String categoryURI) {

        List<String> queries = new ArrayList<String>();
        String search = strip(categoryURI);


        String[] array = search.split(" ");
        for (int i = 0; i < array.length; i++) {
            StringBuffer buf = new StringBuffer();
            for (int k = i; k < array.length; k++) {
                buf.append(array[k] + " ");
            }
            queries.add(one(buf.toString(), categoryURI));
        }

        return queries;
    }


    /**
     * pos and negative examples found by navigation
     *
     * SELECT distinct ?pos ?neg WHERE {
     * <http://dbpedia.org/resource/Alfred_Jules_Ayer> <http://dbpedia.org/property/wikilink> ?neg .
     * ?neg skos:subject ?cat. Filter (?cat !=   <http://dbpedia.org/resource/Category:Alumni_of_Magdalen_College%2C_Oxford> ) .
     * ?neg <http://dbpedia.org/property/wikilink> ?pos .
     * ?pos a owl:Thing .
     * ?pos skos:subject <http://dbpedia.org/resource/Category:Alumni_of_Magdalen_College%2C_Oxford> .
     *  Filter (?pos != <http://dbpedia.org/resource/Alfred_Jules_Ayer> )}
     *  LIMIT 1000
     * 
     * @param resourceUri
     * @param categoryUri
     * @return
     */
    public static List<String> getNavigationQueries(String resourceUri, String categoryUri) {
        List<String> queries = new ArrayList<String>();
        String s1 =  "SELECT distinct ?pos ?neg WHERE {" +
                        "<"+resourceUri+"> <http://dbpedia.org/property/wikilink> ?neg ." +
                        "?neg skos:subject ?cat. " +
                        "Filter (?cat !=   <"+categoryUri+"> ) . " +
                        "?neg <http://dbpedia.org/property/wikilink> ?pos ." +
                        "?pos a owl:Thing ." +
                        "?pos skos:subject <"+categoryUri+"> . " +
                        "Filter (?pos != <"+resourceUri+"> )" + "} LIMIT 1000";

        String s2 =  "SELECT distinct ?pos ?neg WHERE {" +
                "<"+resourceUri+"> <http://dbpedia.org/property/wikilink> ?neg2 ." +
                "?neg2 <http://dbpedia.org/property/wikilink> ?neg ." +
                "?neg skos:subject ?cat. " +
                "Filter (?cat !=   <"+categoryUri+"> ) . " +
                "?neg <http://dbpedia.org/property/wikilink> ?pos ." +
                "?pos a owl:Thing ." +
                "?pos skos:subject <"+categoryUri+"> . " +
                "Filter (?pos != <"+resourceUri+"> )" + "} LIMIT 1000";
        queries.add(s1);
        queries.add(s2);
        return queries;
    }


    private static String strip(String categoryURI) {
        String s = categoryURI.substring("http://dbpedia.org/resource/Category:".length());
        try {
            s = URLDecoder.decode(s, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return s.replaceAll("[^a-zA-Z]", " ").replaceAll("_", " ").replaceAll("  ", " ");

    }

    private static String one(String search, String categoryURI) {
        String bif = new BifContains(search).makeWithAnd();
        return "Select distinct ?s { " + "?s rdfs:comment ?comment . " + "?s a owl:Thing . " +
                // ((posNeg)?"?s skos:subject <"+categoryURI+"> .":"?s skos:subject ?cat .Filter (?cat !=<"+categoryURI+">) .") +
                "FILTER (bif:contains (?comment, '" + bif + "' )) } LIMIT 1000";
    }
}
